﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;


namespace Silver
{
    public partial class Form1 : Form
    {
        bool capture = false;
        int prevMouseX = 0;
        int prevMouseY = 0;

        public Form1()
        {
            InitializeComponent();
            this.TopMost = true;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            Image bg = Image.FromFile(@"..\..\..\img\bg.png");
            this.groupBox1.BackgroundImage = bg;
            this.groupBox1.BackgroundImageLayout = ImageLayout.None;
           // this.BackgroundImage = bg;
            //this.BackgroundImageLayout = ImageLayout.Center;
            int width = bg.Width;
            int height = bg.Height;
            this.Width = width - 80;
            this.Height = height - 60;
            this.StartPosition = FormStartPosition.Manual;
            this.Top = Screen.PrimaryScreen.Bounds.Height - height;
            this.Left = Screen.PrimaryScreen.Bounds.Width - width;
            this.groupBox1.Click += new EventHandler(Form1_Click);
            this.groupBox1.MouseDown += new MouseEventHandler(Form1_MouseDown);
            this.groupBox1.MouseUp += new MouseEventHandler(Form1_MouseUp);
            this.groupBox1.MouseMove+=new MouseEventHandler(Form1_MouseMove);
            this.Click += new EventHandler(Form1_Click);
            this.MouseDown += new MouseEventHandler(Form1_MouseDown);
             this.MouseUp += new MouseEventHandler(Form1_MouseUp);
            this.MouseMove += new MouseEventHandler(Form1_MouseMove);
            
          
        }

        void Form1_Click(object sender, EventArgs e)
        {
            MouseEventArgs arg = e as MouseEventArgs;
            if (arg.Button == System.Windows.Forms.MouseButtons.Right)
            {
               contextMenuStrip2.Show(sender as Control, arg.Location);
               
            }

        }

        void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            prevMouseX = e.X;
            prevMouseY = e.Y;
            capture = true;

        }

        void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            capture = false;
        }

        void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (capture)
            {
                this.Left += e.X - prevMouseX;
                this.Top = e.Y - prevMouseY;
            }
        }

        private void exitToolScriptMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }



            //RMB
            //deserializedSObject[0].variety.name 工行纸白银人民币报价
            //deserializedSObject[0].message.middlePrice 中间价
            //deserializedSObject[0].message.topMiddlePrice 最高中间价
            //deserializedSObject[0].message.lowMiddlePrice 最低中间价
            //deserializedSObject[0].message.upDrop 涨跌价
            //deserializedSObject[0].message.buy 银行买入价
            //deserializedSObject[0].message.sell 银行卖出价
            //deserializedSObject[0].message.revTime 报价时间


            //DateTime dt = new DateTime(deserializedSObject[0].message.revTime.time);
            // DateTime dt2 = new DateTime(deserializedSObject[0].variety.priceDate.time);


            //var a = new DateTime(1970, 01, 01).AddMilliseconds(deserializedSObject[0].variety.priceDate.time);
            //var b = new DateTime(1970, 01, 01).AddMilliseconds(deserializedSObject[0].message.revTime.time).AddMinutes(deserializedSObject[0].message.revTime.timezoneOffset * -1);


        private void Form1_Load(object sender, EventArgs e)
        {

            StartThread thr = new StartThread();
            thr.SetUIData += new SetUI(SetUIMethod1);
            thr.ThreadStart();

            /* Thread th = new Thread(new ThreadStart(GetSiverData));
             th.IsBackground = true;
             th.Start();*/
        }

        


       /* public void SetUIData(object value, Control control)
        {

            control.Text = value.ToString();

        }*/
        public void SetUIMethod1(object sell_value,object buy_value)
        {
            this.textBox_buy.Text = buy_value.ToString();
            //this.textBox_revtime.Text = revtime_value.ToString();
            this.textBox_sell.Text = sell_value.ToString();

            if (this.textBox_setprice.Text != string.Empty && this.textBox_set_sellprice.Text == string.Empty)
            {
                 this.CompareData(Convert.ToSingle(buy_value), Convert.ToSingle(this.textBox_setprice.Text));

            }
           if (this.textBox_set_sellprice.Text != string.Empty&& this.textBox_setprice.Text == string.Empty)
           {
               this.CompareData1(Convert.ToSingle(buy_value), Convert.ToSingle(this.textBox_set_sellprice.Text));
           }

            
        }

        public void CompareData(float value1, float value2)
        {

            if (value1 < value2 || value1 == value2)
                MessageBox.Show("your set is lower,you can buy it");
     
        }

        public void CompareData1(float value1, float value2)
        {

            if (value1 > value2 || value1 == value2)
                MessageBox.Show("ok,you can sell it");

        }


       

        
       /* public void GetSiverData()
        {
            while (true)
            {

                System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"http://tool.cngold.org/plus/qjson.do?codes=CNYAAG,USDAAG&names=%E5%B7%A5%E8%A1%8C%E7%BA%B8%E7%99%BD%E9%93%B6%E4%BA%BA%E6%B0%91%E5%B8%81%E6%8A%A5%E4%BB%B7,%E5%B7%A5%E8%A1%8C%E7%BA%B8%E7%99%BD%E9%93%B6%E7%BE%8E%E5%85%83%E6%8A%A5%E4%BB%B7");
                HttpWebResponse myres = (HttpWebResponse)request.GetResponse();
                Stream resStream = myres.GetResponseStream();
                StreamReader sr = new StreamReader(resStream, System.Text.Encoding.UTF8);

                string html = sr.ReadToEnd();
                sr.Close();

                SObject[] deserializedSObject = JsonConvert.DeserializeObject<SObject[]>(html);

         

                this.Invoke(new SetUI(SetUIData),deserializedSObject[0].message.buy.ToString(),this.textBox_buy);
                
                   //temp = deserializedSObject[0].message.sell.ToString();

                   this.Invoke(new SetUI(SetUIData), deserializedSObject[0].message.sell.ToString(), this.textBox_sell);
                   var b = new DateTime(1970, 01, 01).AddMilliseconds(deserializedSObject[0].message.revTime.time).AddMinutes(deserializedSObject[0].message.revTime.timezoneOffset * -1);
                   this.Invoke(new SetUI(SetUIData),b,this.textBox_revtime);


                Thread.Sleep(1000 * 5);
            }
    
        }*/



    }
    public delegate void SetUI(object value1,object value2);

}
